package com.icecola.d5_synchronized_method;

public class Account {
    private String cardId; //卡号
    private double money; //余额

    public String getCardId() {
        return cardId;
    }

    public Account() {
    }

    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }
    // 同步方法 隐含的锁 —— this 锁整个方法
    // 范围 同步方法锁的范围大 性能相对差
    // 可读性 同步方法好
    public synchronized void drawMoney(double money) {
        String name = Thread.currentThread().getName();
        if(this.money >= money){
            System.out.println("当前余额"+this.money);
            this.money -= money;
            System.out.println(name+"取钱成功，余额:"+this.money);
        }else {
            System.out.println(name+"余额不足");
        }

    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }


}
